﻿#region references
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
#endregion

namespace IcaGames.CustomControls
{
    /*
     * My own custom Registration form class.
     * This Control is used when a new user wants to register an account on this Website.
     */ 
    [DefaultProperty("Text"), ToolboxData("<{0}:CustomCreateUser runat=\"server\"></{0}:CustomCreateUser")]
    public class CreateUser : WebControl, INamingContainer
    {
        public EventHandler Click;

        #region private attributes
        private Label lblEmail;
        private Label lblPassword;
        private Label lblConfirmPassword;
        private Label lblPrefix;
        private Label lblFirstName;
        private Label lblLastName;
        private TextBox txtEmail;
        private TextBox txtPassword;
        private TextBox txtConfirmPassword;
        private TextBox txtPrefix;
        private TextBox txtFirstName;
        private TextBox txtLastName;
        private Button btnCreateUser;
        private RequiredFieldValidator rfvEmail;
        private RequiredFieldValidator rfvPassword;
        private RequiredFieldValidator rfvConfirmPassword;
        private RequiredFieldValidator rfvFirstName;
        private RequiredFieldValidator rfvLastName;
        private ValidationSummary valSummary;
        #endregion

        #region public attributes
        /* lblEmail properties */
        [Bindable(false),
        Category("Labels"),
        DefaultValue(""),
        Description("The e-mail label")]
        public string LabelEmail
        {
            get
            {
                this.EnsureChildControls();
                return HttpContext.Current.Server.HtmlEncode(this.lblEmail.Text);
            }
            set
            {
                this.EnsureChildControls();
                this.lblEmail.Text = value;
            }
        }

        /* lblPassword properties */
        [Bindable(false),
        Category("Labels"),
        DefaultValue(""),
        Description("The password label")]
        public string LabelPassword
        {
            get
            {
                this.EnsureChildControls();
                return HttpContext.Current.Server.HtmlEncode(this.lblPassword.Text);
            }
            set
            {
                this.EnsureChildControls();
                this.lblPassword.Text = value;
            }
        }

        /* lblConfirmPassword properties */
        [Bindable(false),
        Category("Labels"),
        DefaultValue(""),
        Description("The confirm password label")]
        public string LabelConfirmPassword
        {
            get 
            {
                this.EnsureChildControls();
                return HttpContext.Current.Server.HtmlEncode(this.lblConfirmPassword.Text);
            }
            set 
            {
                this.EnsureChildControls();
                this.lblConfirmPassword.Text = value;
            }
        }

        /* lblPrefix properties */
        [Bindable(false),
        Category("Labels"),
        DefaultValue(""),
        Description("The prefix label")]
        public string LabelPrefix
        {
            get
            {
                this.EnsureChildControls();
                return HttpContext.Current.Server.HtmlEncode(this.lblPrefix.Text);
            }
            set
            {
                this.EnsureChildControls();
                this.lblPrefix.Text = value;
            }
        }

        /* lblFirstName properties */
        [Bindable(false),
        Category("Labels"),
        DefaultValue(""),
        Description("The firstname label")]
        public string LabelFirstName
        {
            get
            {
                this.EnsureChildControls();
                return HttpContext.Current.Server.HtmlEncode(this.lblFirstName.Text);
            }
            set
            {
                this.EnsureChildControls();
                this.lblFirstName.Text = value;
            }
        }

        /* lblFirstName properties */
        [Bindable(false),
        Category("Labels"),
        DefaultValue(""),
        Description("The lastname label")]
        public string LabelLastName
        {
            get
            {
                this.EnsureChildControls();
                return HttpContext.Current.Server.HtmlEncode(this.lblLastName.Text);
            }
            set
            {
                this.EnsureChildControls();
                this.lblLastName.Text = value;
            }
        }

        /* txtEmail properties */
        [Bindable(false),
        Category("TextBoxes"),
        DefaultValue(""),
        Description("The e-mail TextBox")]
        public string TextBoxEmail
        {
            get 
            {
                this.EnsureChildControls();
                return HttpContext.Current.Server.HtmlEncode(this.txtEmail.Text);
            }
        }

        /* txtPassword properties */
        [Bindable(false),
        Category("TextBoxes"),
        DefaultValue(""),
        Description("The password TextBox")]
        public string TextBoxPassword
        {
            get
            {
                this.EnsureChildControls();
                return HttpContext.Current.Server.HtmlEncode(this.txtPassword.Text);
            }
        }

        /* txtPrefix properties */
        [Bindable(false),
        Category("TextBoxes"),
        DefaultValue(""),
        Description("The prefix TextBox")]
        public string TextBoxPrefix
        {
            get
            {
                this.EnsureChildControls();
                return HttpContext.Current.Server.HtmlEncode(this.txtPrefix.Text);
            }
        }

        /* txtFirstName properties */
        [Bindable(false),
        Category("TextBoxes"),
        DefaultValue(""),
        Description("The firstname TextBox")]
        public string TextBoxFirstName
        {
            get
            {
                this.EnsureChildControls();
                return HttpContext.Current.Server.HtmlEncode(this.txtFirstName.Text);
            }
        }

        /* txtLastName properties */
        [Bindable(false),
        Category("TextBoxes"),
        DefaultValue(""),
        Description("The lastname TextBox")]
        public string TextBoxLastName
        {
            get
            {
                this.EnsureChildControls();
                return HttpContext.Current.Server.HtmlEncode(this.txtLastName.Text);
            }
        }
        #endregion

        /*
         * Override function that generates the controls that are used.
         */ 
        protected override void CreateChildControls()
        {
            this.Controls.Add(new LiteralControl("<table class=\"registerTable\"" +
                "width=\"100%\"><tr><td style=\"width: 35%\">"));

            this.lblEmail = CreateLabel("lblEmail", "E-mailadres:");
            this.Controls.Add(lblEmail);
            
            this.Controls.Add(new LiteralControl("</td><td>"));

            this.txtEmail = CreateTextBox("txtEmail", "Vul uw e-mailadres in");
            this.Controls.Add(txtEmail);

            this.rfvEmail = CreateValidator("rfvEmail", "E-mailadres is een verplicht veld", "txtEmail");
            this.Controls.Add(new LiteralControl("&nbsp;"));
            this.Controls.Add(rfvEmail);

            this.Controls.Add(new LiteralControl("</td></tr><tr><td>"));

            this.lblPassword = CreateLabel("lblPassword", "Wachtwoord:");
            this.Controls.Add(lblPassword);
            
            this.Controls.Add(new LiteralControl("</td><td>"));

            this.txtPassword = CreateTextBox("txtPassword", "Vul uw wachtwoord in");
            this.txtPassword.TextMode = TextBoxMode.Password;
            this.Controls.Add(txtPassword);

            this.rfvPassword = CreateValidator("rfvPassword", "Wachtwoord is een verplicht veld", "txtPassword");
            this.Controls.Add(new LiteralControl("&nbsp;"));
            this.Controls.Add(this.rfvPassword);

            this.Controls.Add(new LiteralControl("</td></tr><tr><td>"));

            this.lblConfirmPassword = CreateLabel("lblConfirmPassword", "Bevestig wachtwoord:");
            this.Controls.Add(this.lblConfirmPassword);

            this.Controls.Add(new LiteralControl("</td><td>"));

            this.txtConfirmPassword = CreateTextBox("txtConfirmPassword", "Bevestig uw wachtwoord");
            this.txtConfirmPassword.TextMode = TextBoxMode.Password;
            this.Controls.Add(this.txtConfirmPassword);

            this.rfvConfirmPassword = CreateValidator("rfvConfirmPassword", 
                "Bevestig wachtwoord is een verplicht veld", "txtConfirmPassword");
            this.Controls.Add(new LiteralControl("&nbsp;"));
            this.Controls.Add(this.rfvConfirmPassword);

            this.Controls.Add(new LiteralControl("</td></tr><tr><td>"));

            this.lblPrefix = CreateLabel("lblPrefix", "Aanhef:");
            this.Controls.Add(this.lblPrefix);

            this.Controls.Add(new LiteralControl("</td><td>"));

            this.txtPrefix = CreateTextBox("txtPrefix", "Vul uw aanhef in");
            this.Controls.Add(this.txtPrefix);

            this.Controls.Add(new LiteralControl("</td></tr><tr><td>"));

            this.lblFirstName = CreateLabel("lblFirstName", "Voornaam:");
            this.Controls.Add(this.lblFirstName);

            this.Controls.Add(new LiteralControl("</td><td>"));

            this.txtFirstName = CreateTextBox("txtFirstName", "Vul uw voornaam in");
            this.Controls.Add(this.txtFirstName);

            this.rfvFirstName = CreateValidator("rfvFirstName", "Voornaam is een verplicht veld", "txtFirstName");
            this.Controls.Add(new LiteralControl("&nbsp;"));
            this.Controls.Add(this.rfvFirstName);

            this.Controls.Add(new LiteralControl("</td></tr><tr><td>"));

            this.lblLastName = CreateLabel("lblLastName", "Achternaam:");
            this.Controls.Add(this.lblLastName);

            this.Controls.Add(new LiteralControl("</td><td>"));

            this.txtLastName = CreateTextBox("txtLastName", "Vul uw achternaam in");
            this.Controls.Add(this.txtLastName);

            this.rfvLastName = CreateValidator("rfvLastName", "Achternaam is een verplicht veld", "txtLastName");
            this.Controls.Add(new LiteralControl("&nbsp;"));
            this.Controls.Add(this.rfvLastName);

            this.Controls.Add(new LiteralControl("</td></tr><tr><td style=\"padding-top: 10px\">"));

            this.btnCreateUser = new Button();
            this.btnCreateUser.ID = "btnCreateUser";
            this.btnCreateUser.Text = "Registreren";
            this.btnCreateUser.CssClass = "buttonRedLarge";
            this.btnCreateUser.ValidationGroup = "Register";
            this.btnCreateUser.Click += new EventHandler(AddUser);
            this.Controls.Add(this.btnCreateUser);

            this.Controls.Add(new LiteralControl("</td></tr><tr><td colspan=\"2\">"));

            this.valSummary = new ValidationSummary();
            this.valSummary.ID = "valSummary";
            this.valSummary.ValidationGroup = "Register";
            this.valSummary.DisplayMode = ValidationSummaryDisplayMode.BulletList;
            this.valSummary.ShowSummary = true;
            this.valSummary.HeaderText = "<b>De volgende velden zijn niet ingevuld of bevatten fouten:</b>";
            this.Controls.Add(this.valSummary);

            this.Controls.Add(new LiteralControl("</td></tr></table>"));
        }

        /*
         * Click event that is fired when a new user presses the btnCreateUser button.
         */ 
        protected virtual void AddUser(object sender, EventArgs e)
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }

        /*
         * Function to create a Label using a specified ID and text.
         * 
         * @param
         * labelId: The ID of the Label.
         * text: The text of the Label.
         * 
         * @returns
         * A new Label with the specified ID and text.
         */ 
        protected Label CreateLabel(string labelId, string text)
        {
            Label lbl = new Label { ID = labelId, Text = text };
            lbl.Font.Bold = true;

            return lbl;
        }

        /*
         * Function to create a TextBox using a specified ID and tooltip.
         * 
         * @param
         * tbId: The ID of the TextBox.
         * tooltip: The ToolTip of the TextBox.
         * 
         * @returns
         * A new TextBox with the specified ID and tooltip.
         */ 
        protected TextBox CreateTextBox(string tbId, string tooltip)
        {
            TextBox tb = new TextBox
            {
                ID = tbId,
                ToolTip = tooltip,
                CssClass = "textboxLogin"
            };

            return tb;
        }

        /*
         * Function to create a RequiredFieldValidator using a specified ID, errorMsg and ControlToValidate.
         * 
         * @param
         * valId: The ID of the Validator.
         * errorMsg: The error message of the Validator.
         * valControl: The ID of the control that has to be validated.
         * 
         * @returns
         * A RequiredFieldValidator using the specified ID, errorMsg and ControlToValidate.
         */ 
        protected RequiredFieldValidator CreateValidator(string valId, 
            string errorMsg, string valControl)
        {
            RequiredFieldValidator rfv = new RequiredFieldValidator
            {
                ID = valId,
                Text = "*",
                ErrorMessage = errorMsg,
                ValidationGroup = "Register",
                ControlToValidate = valControl,
                Display = ValidatorDisplay.Dynamic,
                SetFocusOnError = true
            };

            return rfv;
        }

        /*
         * Function to clear the textbox fields of the Control.
         */ 
        public void ClearFields()
        {
            this.txtEmail.Text = string.Empty;
            this.txtPassword.Text = string.Empty;
            this.txtPrefix.Text = string.Empty;
            this.txtConfirmPassword.Text = string.Empty;
            this.txtFirstName.Text = string.Empty;
            this.txtLastName.Text = string.Empty;
        }
    }
}
